package top.lyjwn.ajui.activity;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import top.lyjwn.ajui.AJUIApplication;
import top.lyjwn.ajui.R;
import top.lyjwn.ajui.base.BaseActivity;
import top.lyjwn.ajui.data.Token;
import top.lyjwn.ajui.data.User;
import top.lyjwn.ajui.listener.IWebSocketListener;
import top.lyjwn.ajui.network.HttpUtil;
import top.lyjwn.ajui.network.IWebSocketClient;
import top.lyjwn.ajui.network.OnCallback;
import top.lyjwn.ajui.network.PostRequestBody;
import top.lyjwn.ajui.test.UserUtil;
import top.lyjwn.ajui.util.ToastUtil;
import top.lyjwn.ajui.util.TokenUtil;
import top.lyjwn.ajui.util.UrlUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LoginActivity extends BaseActivity {

    private EditText account;

    private EditText password;

    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        account = findViewById(R.id.et_account);
        password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PostRequestBody postRequestBody = new PostRequestBody();
                postRequestBody.setUrl(UrlUtil.HOST+"/login");
                Map<String, Object> map = new ConcurrentHashMap<>();
                map.put("username",account.getText().toString());
                map.put("password",password.getText().toString());
                postRequestBody.setRequestBody(map);
                HttpUtil.post(postRequestBody, new OnCallback() {
                    @Override
                    public void onResponse(boolean done, Integer code, String msg, String data) {
                        if (done){
                            TokenUtil.setToken(JSONObject.parseObject(data).toJavaObject(Token.class));
                            Request.Builder builder = HttpUtil.getBuilder(UrlUtil.WSHOST);
                            Request request = builder.build();
                            AJUIApplication.setChatWebSocket(AJUIApplication.getOkHttpClient().newWebSocket(request, new IWebSocketListener(getContext())));
                            HttpUtil.get(UrlUtil.HOST+"/user/get/by/token", new OnCallback() {
                                @Override
                                public void onResponse(boolean done, Integer code, String msg, String data) {
                                    if (done){
                                        User user = JSONObject.parseObject(data).toJavaObject(User.class);
                                        UserUtil.setUser(user);
                                        Log.d("得到用户",JSONObject.toJSONString(UserUtil.getUser()));
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                ToastUtil.normal(getContext(),"登录成功");
                                                Intent intent = new Intent(getContext(), ChatListActivity.class);
                                                startActivity(intent);
                                            }
                                        });
                                    }
                                }
                            });
                        }else{
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtil.normal(getContext(),msg);
                                }
                            });
                        }

                    }
                });
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_activity_login;
    }
}
